<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Grom Hellscream
 * Date: 10/31/12
 * Time: 6:50 PM
 * To change this template use File | Settings | File Templates.
 */
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport('joomla.form.formfield');

// The class name must always be the same as the filename (in camel case)
class JFormFieldDonviedit extends JFormField {

    //The field class must know its own type through the variable $type.
    protected $type = 'donviedit';

    public function getLabel() {
        $label = '';
        $replace = '';

        // Get the label text from the XML element, defaulting to the element name.
        $text = $this->element['label'] ? (string) $this->element['label'] : (string) $this->element['name'];

        // Build the class for the label.
        $class = !empty($this->description) ? 'hasTip' : '';
        $class = $this->required == true ? $class.' required' : $class;

        // Add replace checkbox
        $replace = '<input type="checkbox" name="update['.$this->name.']" value="1" />';

        // Add the opening label tag and main attributes attributes.
        $label .= '<label id="'.$this->id.'-lbl" for="'.$this->id.'" class="'.$class.'"';

        // If a description is specified, use it to build a tooltip.
        if (!empty($this->description)) {
            $label .= ' title="'.htmlspecialchars(trim(JText::_($text), ':').'::' .
                JText::_($this->description), ENT_COMPAT, 'UTF-8').'"';
        }

        // Add the label text and closing tag.
        $label .= '>'.$replace.JText::_($text).'</label>';

        return $label;
    }

    public function getInput() {
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__donvi AS a');

        $db->setQuery($query);
        $column = $db->loadObjectList();

        $result = '<select id="' . $this->id . '" name = "' . $this->name . '">';

        foreach ($column as $donvi)
        {
            $result .= '<option value="' . $donvi->id . '">' . $donvi->ten . '</option>';
        }

        $result .= '</select>';

        return $result;
    }
}